
package pl.genapp.stocksense;

import pl.genapp.appsense.R;
import pl.genapp.stocksense.libraries.FacebookHelper;
import pl.genapp.stocksense.libraries.TwitterHelper;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class LoginActivity extends Activity {
	
	public static final String SHARED_PREFERENCE_STOCK_SENSE = "stocksense";
	public static final String SP_FIRST_TIME_RUN_KEY = "loginActivityFirstRun";
	
	private Button facebookConnectButton;
	private Button twitterConnectButton;
	private Button skip;
	
	private SharedPreferences preferences;
	private FacebookHelper facebookHelper = new FacebookHelper(this);
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		preferences = getSharedPreferences(SHARED_PREFERENCE_STOCK_SENSE, 0);
		
		setContentView(R.layout.activity_login);
		
		facebookConnectButton = (Button) findViewById(R.id.facebookConnectButton);
		facebookConnectButton.setOnClickListener(onFacebookConnectClickListener);
		
		twitterConnectButton = (Button) findViewById(R.id.twitterConnectButton);
		twitterConnectButton.setOnClickListener(onTwitterConnectClickListener);
		
		skip = (Button) findViewById(R.id.skip);
		skip.setOnClickListener(onSkipClickListener);
		
	}
	
	static void showLoginAsNeeded(Context context) {
		
		SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCE_STOCK_SENSE, 0);
		if (!preferences.getBoolean(SP_FIRST_TIME_RUN_KEY, false)) {
			Intent intent = new Intent(context, LoginActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
			context.startActivity(intent);
		}
		
	}
	
	void goToNextActivity() {
		
		finish();
		
	}
	
	void writeFirstRunValue() {
		
		SharedPreferences.Editor editor = preferences.edit();
		editor.putBoolean(SP_FIRST_TIME_RUN_KEY, true);
		editor.commit();
		
	}
	
	private OnClickListener onSkipClickListener = new OnClickListener() {
		
		public void onClick(View v) {
			
			writeFirstRunValue();
			goToNextActivity();
			
		}
	};
	
	private OnClickListener onFacebookConnectClickListener = new OnClickListener() {
		
		public void onClick(View v) {
			
			facebookHelper.logIn(facebookHelper.new CustomFbDialogListener() {
				
			});
			
		}
	};
	
	private OnClickListener onTwitterConnectClickListener = new OnClickListener() {
		
		public void onClick(View v) {
			
			writeFirstRunValue();
			
			TwitterHelper twitterHelper = new TwitterHelper(LoginActivity.this);
			twitterHelper.loginToTwitter();
			
		}
	};
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if (requestCode == FacebookHelper.FACEBOOK_RESPONSE) {
			
			facebookHelper.saveResultToSP(data);
	
			writeFirstRunValue();
			goToNextActivity();
		}
		
	};

	
}
